#!/usr/bin/env ruby

#You might want to change this
ENV["RAILS_ENV"] ||= "production"

require File.dirname(__FILE__) + "/../../config/environment"

$running = true;
Signal.trap("TERM") do 
  $running = false
end

#day,week,month,year counter init
times=0
#day=0
#week=0
#month=0
#year=0
while($running) do
#re calculate the rank (per 5 min)
    i=1
    for blog in Blog.find(:all,:order=>"click DESC")
        blog.update_attribute(:rank , i )
        i+=1
    end
      times+=1
#if today is over(times:60/5*12=144) => empty Blog.day 
#    if this week is over(day:7) => empty Blog.week 
#        if this month is over(week:4) => empty Blog.month
#            if this year is over(month:12) => empty Blog.year

#today is over
#    if times==144
#      for blog_day in Blog.find(:all)
#        blog_day.update_attribute(:day,0)
#      end
#      times=0
#      day+=1
#      #this week is over
#      if day==7
#        for blog in Blog.find(:all)
#          blog.update_attribute(:week,0)
#        end
#        day=0
#        week+=1
        #this month is over
#        if week==4
#          for blog in Blog.find(:all)
#            blog.update_attribute(:month,0)
#          end
#          week=0
#          month+=1
#          #this year is over
#          if month==12
#            for blog in Blog.find(:all)
#              blog.update_attribute(:year,0)
#            end
#            month=0
#            year+=1
#          end #end year
#        end #end month
#     end #end week
#    end #end day
     
  sleep 1800
end
